window.addEvents({
	'domready':function(){
		$('tongue').setStyles({'left':(getScrollSize().x/2)-50,'top':-30});
	},
	'load': function(){
		var tongue = $('tongue');
		var tongueFx = new Fx.Morph(tongue, {			
		    transition: Fx.Transitions.Quad.easeInOut,
		    link: 'cancel'
		});
		$('menu_area').addEvents({
			'mouseenter':function(e){
				e.stop();
				tongue.tween('top',-23);
				this.addEvent('mousemove',function(e){
					e.stop();
					tongueFx.start({'left':e.client.x-50});
				})
			},
			'mouseleave':function(e){
				e.stop();				
				tongueFx.start({'left':(getScrollSize().x/2)-50,'top':-30});
				this.removeEvents('mousemove');
			},
			'click':function(){
				$('header').tween('height',155);
				this.removeEvents('mousemove');
				this.removeEvents('mouseenter');
			}
		})
	},
	'resize':function(){
		$('tongue').setStyles({'left':(getScrollSize().x/2)-50,'top':-30});
	}
});
var ajaxGet = function(what, params, where){
	new Request.HTML({
		headers: {'If-Modified-Since': 'Thu, 1 Jan 1970 00:00:00 GMT','Content-type':'text/html; charset=utf-8'},
		url: what,
		onRequest:function(){$(document.body).setStyle('cursor','progress');},
		method: 'get',
		evalScripts: true,
		evalResponse: true,
		update: where,
		onSuccess:function(){$(document.body).setStyle('cursor','auto');}
	}).send(params);
}
var ajaxPost = function(form, what, where){
	new Request.HTML({
		url: what,
		onRequest:function(){$(document.body).setStyle('cursor','progress');},
		evalScripts: true,
		evalResponse: true,
		update: where,
		onSuccess:function(){$(document.body).setStyle('cursor','auto');}
	}).post(form);
}
var sendInfo = function(where, what){	
	var b = new TinyAlert({'position':'br',skin:'smoke'});
	b.show('Meddelande',what);
}
Form.Validator.add('validate-pers', {
    errorMsg: 'Ange personnummer i rätt format (ÅÅMMDD-XXXX)',
    test: function(element){
    	return Form.Validator.getValidator('IsEmpty').test(element) || (/^\d{2}([0][1-9]|[1][0-2])([0][1-9]|[1-2][0-9]|[3][0-1])-\d{4}$/i).test(element.get('value'));
    }
});

Form.Validator.add('validate-org', {
    errorMsg: 'Ange organizationsnummer i rätt format (XXXXXX-XXXX)',
    test: function(element){
    	return Form.Validator.getValidator('IsEmpty').test(element) || (/^(\d{6})\-(\d{4})$/i).test(element.get('value'));
    }
});
var sendForm = function(frm){
	var frm=$(frm);	
	var myValidator = new Form.Validator.Inline(frm, {useTitles:true,errorPrefix:''});
	if(myValidator.validate()){
		ajaxPost(frm,frm.action);
	}
}
var sendForm2 = function(frm){
	var frm=$(frm);	
	
		ajaxPost(frm,frm.action);
	
}
var associationsSuggest = function(sok){
	if(sok.length>2){
		$('association_list').fade('in');
		if(this.s==null){
			this.s=new Request();
		}else{
			this.s.cancel();
		}
		this.s=new Request({
			method: 'get', 
			url: 'associations.php',
			data:'search='+sok,
			onSuccess:function(responseText){
				$('association_list').innerHTML=responseText;				
			},
			evalScripts :true,
			evalResponse :false
		}).send();
	}else{
		$('association_list').fade('out');
	}
}
var circlesSuggest = function(sok){
	if(sok.length>2){
		$('circle_list').fade('in');
		if(this.s==null){
			this.s=new Request();
		}else{
			this.s.cancel();
		}
		this.s=new Request({
			method: 'get', 
			url: 'circles.php',
			data:'search='+sok,
			onSuccess:function(responseText){
				$('circle_list').innerHTML=responseText;				
			},
			evalScripts :true,
			evalResponse :false
		}).send();
	}else{
		$('circle_list').fade('out');
	}
}
var personSuggest = function(sok,i){
	if(sok.length>2){
		$('person_list_'+i).fade('in');
		if(this.s==null){
			this.s=new Request();
		}else{
			this.s.cancel();
		}
		this.s=new Request({
			method: 'get', 
			url: 'persons.php',
			data:'search='+sok+'&i='+i,
			onSuccess:function(responseText){
				$('person_list_'+i).innerHTML=responseText;				
			},
			evalScripts :true,
			evalResponse :false
		}).send();
	}else{
		$('person_list_'+i).fade('out');
	}
}
var leaderSuggest = function(sok){
	if(sok.length>2){
		$('leader_list').fade('in');
		if(this.s==null){
			this.s=new Request();
		}else{
			this.s.cancel();
		}
		this.s=new Request({
			method: 'get', 
			url: 'persons.php',
			data:'searchleader='+sok,
			onSuccess:function(responseText){
				$('leader_list').innerHTML=responseText;				
			},
			evalScripts :true,
			evalResponse :false
		}).send();
	}else{
		$('leader_list').fade('out');
	}
}
var fillFormPerson = function(i,id){
	$$('input[name="txt_participant_id_'+i+'"]')[0].value=persons[id].id;
	$$('input[name="txt_participant_firstname_'+i+'"]')[0].value=persons[id].firstname;
	$$('input[name="txt_participant_lastname_'+i+'"]')[0].value=persons[id].lastname;
	$$('input[name="txt_participant_pers_'+i+'"]')[0].value=persons[id].pers;
	$$('input[name="txt_participant_address_'+i+'"]')[0].value=persons[id].address;
	$$('input[name="txt_participant_zip_'+i+'"]')[0].value=persons[id].zip;
	$$('input[name="txt_participant_city_'+i+'"]')[0].value=persons[id].city;
	$$('input[name="txt_participant_tel_'+i+'"]')[0].value=persons[id].tel;
	$('person_list_'+i).fade('out');
	$('person_list_'+i).innerHTML='';
}
var fillFormLeader = function(id){
	$$('input[name="txt_leader_id"]')[0].value=leaders[id].id;
	$$('input[name="txt_user_id"]')[0].value=leaders[id].user_id;
	$$('input[name="ddl_association"]')[0].value=leaders[id].association_id;
	$$('input[name="txt_leader_firstname"]')[0].value=leaders[id].firstname;
	$$('input[name="txt_leader_lastname"]')[0].value=leaders[id].lastname;
	$$('input[name="txt_leader_pers"]')[0].value=leaders[id].pers;
	$$('input[name="txt_leader_address"]')[0].value=leaders[id].address;
	$$('input[name="txt_leader_zip"]')[0].value=leaders[id].zip;
	$$('input[name="txt_leader_city"]')[0].value=leaders[id].city;
	$$('input[name="txt_leader_email"]')[0].value=leaders[id].email;
	$$('input[name="txt_leader_tel"]')[0].value=leaders[id].tel;
	$('leader_list').fade('out');
	$('leader_list').innerHTML='';	
}
var fillForm = function(what,id){
	switch(what){
	case 'associations':
		$$('input[name="txt_association_id"]')[0].value=assoc[id].id;
		$$('input[name="txt_association_name"]')[0].value=assoc[id].name;
		$$('input[name="txt_association_address"]')[0].value=assoc[id].address;
		$$('input[name="txt_association_zip"]')[0].value=assoc[id].zip;
		$$('input[name="txt_association_city"]')[0].value=assoc[id].city;
		$$('input[name="txt_association_org"]')[0].value=assoc[id].org;
		var chks = $('association_member_of_list').getElements('input');		
		chks.each(function(input,i){
			if(assoc[id].member_of.split(',').contains(input.value)){
				input.checked=true;
			}
		})
		$('ddl_association_member_of').value=assoc[id].member_of;
		$('association_list').fade('out');
		$('association_list').innerHTML='';
	break;
	case 'circles':
		$$('input[name="txt_circle_id"]')[0].value=circles[id].id;
		$$('input[name="txt_circle_name"]')[0].value=circles[id].name;
		$$('input[name="txt_circle_topic"]')[0].value=circles[id].topic;
		$$('input[name="txt_circle_material"]')[0].value=circles[id].material;
		$$('input[name="txt_circle_start_date"]')[0].value=circles[id].start_date;
		$$('input[name="txt_circle_start"]')[0].value=circles[id].start;
		$$('input[name="txt_circle_end"]')[0].value=circles[id].end;
		$$('input[name="txt_circle_number_of_meetings"]')[0].value=circles[id].number_of_meetings;
		$$('input[name="txt_circle_place"]')[0].value=circles[id].place;
		$$('input[name="txt_circle_zip"]')[0].value=circles[id].zip;
		$$('input[name="txt_circle_city"]')[0].value=circles[id].city;		
		$('circle_list').fade('out');
		$('circle_list').innerHTML='';
	break;
	}
}
var weekday=new Array(7);
weekday[0]="Söndag";
weekday[1]="Mondag";
weekday[2]="Tisdag";
weekday[3]="Onsdag";
weekday[4]="Törsdag";
weekday[5]="Fredag";
weekday[6]="Lördag";